package com.neusoft.zqygbc.neusoft_wenda.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.activity.QuestionContentActivity;
import com.neusoft.zqygbc.neusoft_wenda.base.RecyclerViewAdapter;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.FollowEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Status;
import com.neusoft.zqygbc.neusoft_wenda.model.FollowModel;

import java.util.List;

import retrofit2.Call;

/**
 * Created by Shinelon on 2017/5/19.
 */

public class FollowAdapter extends RecyclerViewAdapter<FollowAdapter.ViewHolder> {
    private FollowModel followModel;
    private Call call;
    private RetrofitCallback<Status> followCallback;
    private RetrofitCallback<Status> delFollowCallback;
    private String followstatus="1";
    private SharedPreferences sp;

    public FollowAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
        sp = context.getSharedPreferences("userinfo", context.MODE_PRIVATE);
    }

    @Override
    public void onBindViewHolder(final FollowAdapter.ViewHolder holder, final int position) {
        final FollowEntity entity = (FollowEntity) data.get(position);
        holder.myfollow_username.setText(entity.getUsername());
        holder.myfollow_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(followstatus.equals("1")){
                    delFollow(holder,entity.getUid());
                }else{
                    follow(holder,entity.getUid());

                }
            }
        });

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView myfollow_username;
        ImageView myfollow_follow;


        public ViewHolder(View itemView) {
            super(itemView);
            myfollow_username = (TextView) itemView.findViewById(R.id.myfollow_username);
            myfollow_follow = (ImageView) itemView.findViewById(R.id.myfollow_follow);

        }
    }

    private void follow(final FollowAdapter.ViewHolder holder,String quid) {
        followModel = new FollowModel();
        call = followModel.follow(sp.getString("user_id", ""), quid);
        followCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(context, "已关注", Toast.LENGTH_SHORT).show();
                    holder.myfollow_follow.setImageResource(R.drawable.ic_follow_pressed);
                    followstatus = "1";
                } else {
                    Toast.makeText(context, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, followCallback);
    }

    private void delFollow(final FollowAdapter.ViewHolder holder,String quid) {
        followModel = new FollowModel();
        call = followModel.delfollow(sp.getString("user_id", ""), quid);
        delFollowCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(context, "关注取消", Toast.LENGTH_SHORT).show();
                    holder.myfollow_follow.setImageResource(R.drawable.ic_follow_normal);
                    followstatus = "0";
                } else {
                    Toast.makeText(context, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, delFollowCallback);
    }


}

